package com.rongshu.view.discovery.activity;

import android.animation.Animator;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.AnimationUtil;
import com.rongshu.utils.Constant;
import com.rongshu.utils.DateUtils;
import com.rongshu.utils.GlideUtils;
import com.rongshu.utils.LoginUtils;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.discovery.bean.UserInfoResponse;
import com.rongshu.view.discovery.contract.UserHomePageView;
import com.rongshu.view.discovery.presenter.UserHomePagePresenter;
import com.rongshu.view.home.activity.PreviewImgActivity;
import com.rongshu.view.userInfo.activity.DirectChatActivity;
import com.rongshu.view.userInfo.activity.PenpalWillActivity;
import com.rongshu.view.userInfo.activity.RecommendActivity;
import com.rongshu.view.userInfo.activity.ShareActivity;
import com.rongshu.view.userInfo.dialog.ChiefExperienceOfficerDialog;
import com.rongshu.view.userInfo.entity.DirectMessage;
import com.rongshu.widget.PraiseNumDialog;
import com.utils.DensityUtil;
import com.utils.FastClickUtils;
import com.widget.loading.AnimatorListener;
import com.widget.toast.ToastUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * created by zhengweis on 2018/11/2.
 * description：用户主页
 */
public class UserHomePageActivity extends BaseActivity<UserHomePagePresenter> implements UserHomePageView {
    @BindView(R.id.head_left)
    RelativeLayout headLeft;
    @BindView(R.id.user_logo)
    ImageView userLogo;
    @BindView(R.id.vip_flag)
    ImageView vipFlag;
    @BindView(R.id.user_name)
    TextView userName;
    @BindView(R.id.user_sex_tv)
    TextView userSexTv;
    @BindView(R.id.user_sign)
    TextView userSign;
    @BindView(R.id.keep_num)
    TextView keepNum;
    @BindView(R.id.fans_num)
    TextView fansNum;
    @BindView(R.id.penpal_num)
    TextView penpalNum;
    @BindView(R.id.user_city)
    TextView userCity;
    @BindView(R.id.get_praise_num)
    TextView getPraiseNum;
    @BindView(R.id.iv_bg_icon)
    ImageView bpg;
    @BindView(R.id.sex_layout)
    LinearLayout sexLayout;
    @BindView(R.id.city_layout)
    LinearLayout cityLayout;
    @BindView(R.id.article_num)
    TextView articleNum;
    @BindView(R.id.post_num)
    TextView postNum;
    @BindView(R.id.vip_content)
    TextView vipContent;
    @BindView(R.id.layout_detail)
    RelativeLayout layoutDetail;
    @BindView(R.id.iv_chief)
    ImageView ivChief;
    @BindView(R.id.layout_add_attention)
    ImageView addAttentionIcon;
    private int userId;
    private boolean isBlackList = false;//是否在黑名单
    private boolean isConcern = false;//是否关注
    private UserInfoResponse response;

    @Override
    protected int initLayout() {
        return R.layout.activity_user_homepage;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hintTitleBar();
    }

    @Override
    protected void initView() {
        RelativeLayout.LayoutParams layoutParams1 = (RelativeLayout.LayoutParams) headLeft.getLayoutParams();
        layoutParams1.topMargin = DensityUtil.getStateBar(this);
//        RelativeLayout.LayoutParams layoutParams2 = (RelativeLayout.LayoutParams) headRight.getLayoutParams();
//        layoutParams2.topMargin = DensityUtil.getStateBar(this);
        headLeft.setLayoutParams(layoutParams1);
//        headRight.setLayoutParams(layoutParams2);

        RelativeLayout.LayoutParams layoutParams3 = (RelativeLayout.LayoutParams) bpg.getLayoutParams();
        int screenWidth = DensityUtil.getScreenWidth(this);
        final int bpgHight = screenWidth / 4 * 3;
        layoutParams3.height = bpgHight;
        bpg.setLayoutParams(layoutParams3);

        layoutDetail.post(new Runnable() {
            @Override
            public void run() {
                RelativeLayout.LayoutParams layoutParams4 = (RelativeLayout.LayoutParams) layoutDetail.getLayoutParams();
                int detailHight = layoutDetail.getHeight();
                layoutParams4.topMargin = bpgHight - detailHight / 2;
                layoutDetail.setLayoutParams(layoutParams4);
            }
        });

        headLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra(Constant.IS_COLLECT, isConcern);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

    @Override
    protected void initData() {
        userId = getIntent().getIntExtra(Constant.USER_ID, 0);
        setPresenter(new UserHomePagePresenter(this), this);
        presenter.isAtUserBlack(userId);
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @Override
    public void callBackUserMsg(UserInfoResponse response) {
        if (response != null && response.getResponse() != null) {
            this.response = response;
            UserInfoResponse.ResponseBean user = response.getResponse();
            userLogo.setBackground(ContextCompat.getDrawable(UserHomePageActivity.this, R.drawable.shape_stroke_white_background_5dp));
            GlideUtils.loadImageView(UserHomePageActivity.this, bpg, user.getBgp(), R.drawable.user_top_bg, R.drawable.user_top_bg);
            GlideUtils.loadImageView(UserHomePageActivity.this, userLogo, user.getAvatar(), R.drawable.user_head_icon, R.drawable.user_head_icon);
            vipFlag.setVisibility(user.getVip() ? View.VISIBLE : View.GONE);
            userName.setText(user.getBi_name());
            userSign.setText(TextUtils.isEmpty(user.getSignature()) ? "这个人很懒，什么都没有留下。" : user.getSignature());
            if (TextUtils.equals("男", user.getSex())) {
                userSexTv.setText(user.getSex());
            } else if (TextUtils.equals("女", user.getSex())) {
                userSexTv.setText(user.getSex());
            }
            sexLayout.setVisibility(TextUtils.isEmpty(user.getSex()) ? View.GONE : View.VISIBLE);
            keepNum.setText(String.valueOf(user.getGuanzhu()));
            fansNum.setText(String.valueOf(user.getFensi()));
            penpalNum.setText(String.valueOf(user.getPenPalwillCount()));
            userCity.setText(TextUtils.isEmpty(user.getCity()) ? "暂无" : user.getCity());
            cityLayout.setVisibility(TextUtils.isEmpty(user.getCity()) ? View.GONE : View.VISIBLE);
            getPraiseNum.setText(String.valueOf(user.getArticleLikeCount()));
            addAttentionIcon.setBackgroundResource(user.getIs_guanzhu() == 1 ? R.drawable.already_attention : R.drawable.add_attention);
            isBlackList = user.getIs_blacklist() == 1;
            isConcern = user.getIs_guanzhu() == 1;
            articleNum.setText(user.getComeOutCount() + "");
            postNum.setText(user.getPostedCount() + "");
            vipContent.setVisibility(!user.getVip() ? View.GONE : View.VISIBLE);
            vipContent.setText(user.getVipText());
            //首席
            String reg_time = user.getReg_time();
            long registration_date = DateUtils.parseDate(DateUtils.YYYY_MM_DD, reg_time).getTime();
            String time = "2019-02-25";
            long reduced_date = DateUtils.parseDate(DateUtils.YYYY_MM_DD, time).getTime();
            if (registration_date <= reduced_date) {
                ivChief.setVisibility(View.VISIBLE);
            } else {
                ivChief.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void blackListOption() {
        String msg = isBlackList ? "移除" : "加入";
        isBlackList = !isBlackList;
        toast(msg + "成功");
    }

    @Override
    public void concernSuccess() {
        isConcern = !isConcern;
        response.getResponse().setIs_guanzhu(isConcern ? 1 : 0);
        addAttentionIcon.setBackgroundResource(isConcern ? R.drawable.already_attention : R.drawable.add_attention);
    }

    @Override
    public void noPersimmion() {
        finish();
    }

    @OnClick({R.id.get_praise_layout, R.id.send_message, R.id.layout_add_attention, R.id.keep_layout, R.id.fans_layout, R.id.penpal_Layout,
            R.id.article_layout, R.id.note_layout, R.id.recommend_layout, R.id.share_layout, R.id.iv_chief, R.id.iv_bg_icon, R.id.user_logo})
    public void onViewClicked(final View view) {
        if (response == null) {
            return;
        }
        UserInfoResponse.ResponseBean userInfo = this.response.getResponse();
        Intent intent = null;
        switch (view.getId()) {
            case R.id.get_praise_layout:
                PraiseNumDialog.Builder builder = new PraiseNumDialog.Builder(this);
                builder.setType(1);
                builder.setPrarseNum(userInfo.getArticleLikeCount());
                PraiseNumDialog praiseNumDialog = builder.create();
                praiseNumDialog.show();
                break;
            case R.id.send_message:
                if (UserInfoUtils.getUserInfo() == null) {
                    LoginUtils.toLogin(this);
                } else {
                    intent = new Intent(this, DirectChatActivity.class);
                    DirectMessage.ResponseBean bean = new DirectMessage.ResponseBean();
                    bean.setBi_name(userInfo.getBi_name());
                    bean.setNewSend_userid(userInfo.getUser_id_to());
                    intent.putExtra(Constant.INTENT_DATA, bean);
                    startActivity(intent);
                }
                break;
            case R.id.layout_add_attention:
                if (UserInfoUtils.getUserInfo() == null) {
                    LoginUtils.toLogin(this);
                } else if (userInfo.getIs_allow_me() == 0 && userInfo.getIs_guanzhu() == 0) {
                    ToastUtils.show(R.string.attention_no_permission);
                } else if (!FastClickUtils.isFastClick()) {
                    if (userInfo.getIs_guanzhu() == 1) {
                        AnimationUtil.AlphaScaleShrinkAnimator(view, new AnimatorListener() {
                            @Override
                            public void onAnimationEnd(Animator animation) {
                                view.animate().alpha(1).scaleX(1).scaleY(1);
                            }
                        });
                        presenter.cancelConcernWith();
                    } else {
                        AnimationUtil.AlphaScaleAnimator(view, new AnimatorListener() {
                            @Override
                            public void onAnimationEnd(Animator animation) {
                                view.animate().alpha(1).scaleX(1).scaleY(1);
                            }
                        });
                        presenter.ConcernWith();
                    }
                }
                break;
            case R.id.keep_layout://TA的关注
                intent = new Intent(this, TaAttentionAcitivity.class);
                intent.putExtra(Constant.USER_ID, userInfo.getUser_id_to());
                intent.putExtra(Constant.INTENT_TYPE, TaAttentionAcitivity.ATTENTION);
                startActivity(intent);
                break;
            case R.id.fans_layout://TA的粉丝
                intent = new Intent(this, TaAttentionAcitivity.class);
                intent.putExtra(Constant.USER_ID, userInfo.getUser_id_to());
                intent.putExtra(Constant.INTENT_TYPE, TaAttentionAcitivity.FANS);
                startActivity(intent);
                break;
            case R.id.penpal_Layout://笔友会
                if (userInfo.getIs_allow_browse_mypen() == 1) {
                    intent = new Intent(this, PenpalWillActivity.class);
                    intent.putExtra(Constant.USER_ID, userInfo.getUser_id_to());
                    startActivity(intent);
                } else {
                    toast(getResources().getString(R.string.visit_penpal_no_permission));
                }
                break;
            case R.id.article_layout://文章
                intent = new Intent(this, UserArticleActivity.class);
                intent.putExtra(Constant.USER_ID, userInfo.getUser_id_to());
                intent.putExtra(Constant.INTENT_TYPE, Constant.ARTICLE);
                startActivity(intent);
                break;
            case R.id.note_layout://帖子
                intent = new Intent(this, UserArticleActivity.class);
                intent.putExtra(Constant.USER_ID, userInfo.getUser_id_to());
                intent.putExtra(Constant.INTENT_TYPE, Constant.POST);
                startActivity(intent);
                break;
            case R.id.recommend_layout://推荐
                intent = new Intent(this, RecommendActivity.class);
                intent.putExtra(Constant.USER_ID, userInfo.getUser_id_to());
                startActivity(intent);
                break;
            case R.id.share_layout://分享
                intent = new Intent(this, ShareActivity.class);
                intent.putExtra(Constant.USER_ID, userInfo.getUser_id_to());
                startActivity(intent);
                break;
            case R.id.iv_chief:
                ChiefExperienceOfficerDialog.Builder builder1 = new ChiefExperienceOfficerDialog.Builder(this);
                ChiefExperienceOfficerDialog dialog = builder1.create();
                dialog.show();
                break;
            case R.id.iv_bg_icon:
                if (!TextUtils.isEmpty(response.getResponse().getBgp())) {
                    ArrayList<String> listA = new ArrayList<>();
                    listA.add(response.getResponse().getBgp());
                    intent = new Intent(this, PreviewImgActivity.class);
                    intent.putStringArrayListExtra(Constant.INTENT_DATA, listA);
                    startActivity(intent);
                } else {
                    toast("用户还没有设置过背景");
                }
                break;
            case R.id.user_logo:
                if (!TextUtils.isEmpty(response.getResponse().getAvatar())) {
                    ArrayList<String> listB = new ArrayList<>();
                    listB.add(response.getResponse().getAvatar());
                    intent = new Intent(this, PreviewImgActivity.class);
                    intent.putStringArrayListExtra(Constant.INTENT_DATA, listB);
                    startActivity(intent);
                } else {
                    toast("用户还没有设置过头像");
                }
                break;
        }
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra(Constant.IS_COLLECT, isConcern);
        setResult(RESULT_OK, intent);
        super.onBackPressed();
    }
}
